﻿using Syc.Code;
using Syc.Web.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Syc.Domain;
using Syc.Application;

namespace Syc.Web.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            ViewBag.title = "欢迎登陆";
            return View();
        }

        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult CheckLogin(string username, string password, string code)
        {
            LogEntity logEntity = new LogEntity();
            logEntity.LogType = 1;
            try
            {
                if (Session["syc_session_verifycode"].IsEmpty() || Md5.md5(code.ToLower(), 16) != Session["syc_session_verifycode"].ToString())
                {
                    throw new Exception("验证码错误，请重新输入");
                }
                UsersEntity userEntity = new UserApp().CheckLogin(username, password);
                if (userEntity != null)
                {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = userEntity.Id;
                    operatorModel.UserCode = userEntity.UserCode;
                    operatorModel.UserName = userEntity.UserName;
                    operatorModel.CompanyId = "";
                    operatorModel.DepartmentId = "";
                    operatorModel.RoleId = "";
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());
                    if (userEntity.UserAdmin == 1)
                    {
                        operatorModel.IsSystem = true;
                    }
                    else
                    {
                        operatorModel.IsSystem = false;
                    }
                    OperatorProvider.Provider.AddCurrent(operatorModel);
                    logEntity.UserCode = userEntity.UserCode;
                    logEntity.LogResult = true;
                    logEntity.Description = "登录成功";
                    //new LogApp().WriteDbLog(logEntity);
                }
                return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功。" }.ToJson());
            }
            catch (Exception ex)
            {
                logEntity.UserCode = username;
                logEntity.LogResult = true;
                logEntity.Description = "登录失败，" + ex.Message;
                //new LogApp().WriteDbLog(logEntity);
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }
    }
}